//
//  HWGameClassifyGameCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/3/3.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWGameClassifyGameCell: UITableViewCell {
    var logoImageView:UIImageView!
    var nameLabel:UILabel!
    var starImageView:UIImageView!
    var scoreLabel:UILabel!
    var typeLabel:UILabel!
    var sizeLabel:UILabel!
    var centerImageView:UIImageView!
    var playerImageView:UIImageView!
    var detailLabel:UILabel!
    var textTagCollectionView:TTGTextTagCollectionView!
    // 折扣
    var onSaleLabel:UILabel!
    var gameImageViewClick:((_:UIImageView,_:HWClassifyGameModel,_:HWGameClassifyGameCell)->())?
    var model:HWClassifyGameModel?{
        didSet {
            logoImageView.sd_setImage(with: URL(string: model?.icon ?? ""), completed: nil)
            centerImageView.sd_setImage(with: URL(string: model?.video_img ?? ""), completed: nil)
            nameLabel.text = model?.name
            typeLabel.text = model?.type
            scoreLabel.text = model?.point
            sizeLabel.text = model?.size
            if (model?.zhekou.count ?? 0) > 0 {
                onSaleLabel.isHidden = false
                if model?.zhekou.hasSuffix("折") == true {
                    onSaleLabel.text = " \(model?.zhekou ?? "") "
                }else{
                    onSaleLabel.text = " \(model?.zhekou ?? "")折 "
                }
                if onSaleLabel.text == " 10折 " {
                    onSaleLabel.isHidden = true
                }
            }else {
                onSaleLabel.isHidden = true
            }
            if (model?.video_img.count ?? 0) > 0 {
                centerImageView.isHidden = false
                centerImageView.snp.updateConstraints { make in
                    make.top.equalTo(logoImageView.snp.bottom).offset(8*widthScale)
                    make.bottom.equalTo(-10*widthScale)
                    make.height.equalTo(158*widthScale)
                }
            }else {
                centerImageView.isHidden = true
                centerImageView.snp.updateConstraints { make in
                    make.top.equalTo(logoImageView.snp.bottom)
                    make.height.equalTo(0)
                }
            }
            
            textTagCollectionView.removeAllTags()
            for (idx,txt) in (model?.biaoqian ?? []).enumerated() {
                let content = TTGTextTagStringContent.init(text: txt)
                content.textFont = UIFont.systemFont(ofSize: 9*widthScale)
                let normalStyle = TTGTextTagStyle.init()
                normalStyle.cornerRadius = 2*widthScale
                normalStyle.extraSpace = CGSize.init(width: 8*widthScale, height: 4*widthScale)
                normalStyle.backgroundColor = .white
                normalStyle.shadowColor = .white
                if model?.video_url.count ?? 0 > 0 {
                    playerImageView.isHidden = false
                }else {
                    playerImageView.isHidden = true
                }
                if idx == 0 {
                    content.textColor = UIColor.colorWithHexString(hexString: "#FD9450")
                    normalStyle.borderColor = UIColor.colorWithHexString(hexString: "#FE6A4C")
                }else if idx == 1 {
                    content.textColor = UIColor.colorWithHexString(hexString: "#2B66FB")
                    normalStyle.borderColor = UIColor.colorWithHexString(hexString: "#2B66FB")
                }else if idx == 2 {
                    content.textColor = UIColor.colorWithHexString(hexString: "#7936FB")
                    normalStyle.borderColor = UIColor.colorWithHexString(hexString: "#7936FB")
                }else if idx == 3 {
                    content.textColor = UIColor.colorWithHexString(hexString: "#444444")
                    normalStyle.borderColor = UIColor.colorWithHexString(hexString: "#444444")
                }else{
                    content.textColor = UIColor.colorWithHexString(hexString: "#CEA224")
                    normalStyle.borderColor = UIColor.colorWithHexString(hexString: "#CEA224")
                }
                
                let tag = TTGTextTag.init()
                tag.content = content
                tag.style = normalStyle
                textTagCollectionView.addTag(tag)
            }
        }
    }
    
    class func cellWithTableView(tableView:UITableView) -> (HWGameClassifyGameCell) {
        let reUse:String = "HWGameClassifyGameCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWGameClassifyGameCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWGameClassifyGameCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        logoImageView = UIImageView()
        logoImageView.backgroundColor = .clear
        logoImageView.layer.cornerRadius = 10*widthScale
        logoImageView.layer.masksToBounds = true
        logoImageView.contentMode = .scaleAspectFill
        contentView.addSubview(logoImageView)
        
        nameLabel = HWLabel.createBoldLabel(title: "", color: "#333333", font: 14*widthScale)
        contentView.addSubview(nameLabel)
        
        onSaleLabel = HWLabel.createLabel(title: "", color: "FFFFFF", font: 10*widthScale)
        onSaleLabel.layer.cornerRadius = 3
        onSaleLabel.backgroundColor = .red
        onSaleLabel.layer.masksToBounds = true
        contentView.addSubview(onSaleLabel)
        
        centerImageView = UIImageView()
        centerImageView.backgroundColor = .clear
        centerImageView.layer.cornerRadius = 10*widthScale
        centerImageView.layer.masksToBounds = true
        centerImageView.isUserInteractionEnabled = true
        centerImageView.contentMode = .scaleAspectFill
        contentView.addSubview(centerImageView)
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(imageViewTap(_:)))
        centerImageView.addGestureRecognizer(tap)
        
        playerImageView = UIImageView()
        playerImageView.image = UIImage(named: "btn_video")
        centerImageView.addSubview(playerImageView)
        
        starImageView = UIImageView()
        starImageView.image = UIImage(named: "homeStar")
        contentView.addSubview(starImageView)
        
        scoreLabel = HWLabel.createBoldLabel(title: "", color: "#999999", font: 10*widthScale)
        contentView.addSubview(scoreLabel)
        
        typeLabel = HWLabel.createBoldLabel(title: "", color: "#999999", font: 10*widthScale)
        contentView.addSubview(typeLabel)
        
        sizeLabel = HWLabel.createLabel(title: "", color: "#999999", font: 10*widthScale)
        contentView.addSubview(sizeLabel)
        
        textTagCollectionView = TTGTextTagCollectionView.init(frame: .zero)
        textTagCollectionView.delegate = self;
        textTagCollectionView.alignment = TTGTagCollectionAlignment.left;
        textTagCollectionView.numberOfLines = 1
        textTagCollectionView.manualCalculateHeight = true;
        textTagCollectionView.scrollDirection = .horizontal
        textTagCollectionView.showsHorizontalScrollIndicator = false
        contentView.addSubview(textTagCollectionView)
        
        detailLabel = HWLabel.createLabel(title: "详情", color: "#FE6A4C", font: 12*widthScale)
        detailLabel.layer.borderColor = UIColor.colorWithHexString(hexString: "#FE6A4C").cgColor
        detailLabel.layer.borderWidth = 1*widthScale
        detailLabel.layer.cornerRadius = 13*widthScale
        detailLabel.textAlignment = .center
        contentView.addSubview(detailLabel)
    }
    
    func initFrame() -> Void {
        logoImageView.snp.makeConstraints { make in
            make.left.equalTo(12*widthScale)
            make.top.equalTo(3*widthScale).priority(.low)
            make.width.height.equalTo(66*widthScale)
        }
        
        centerImageView.snp.makeConstraints { make in
            make.left.equalTo(12*widthScale)
            make.right.equalTo(-12*widthScale)
            make.top.equalTo(logoImageView.snp.bottom).offset(8*widthScale)
            make.bottom.equalTo(-10*widthScale)
            make.height.equalTo(158*widthScale)
        }
        
        playerImageView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.height.equalTo(40*widthScale)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(logoImageView.snp.right).offset(10*widthScale)
            make.top.equalTo(logoImageView).offset(3*widthScale)
            
        }
        
        onSaleLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.right).offset(10*widthScale)
            make.right.lessThanOrEqualTo(-10*widthScale)
            make.height.equalTo(13*widthScale)
            make.centerY.equalTo(nameLabel)
        }
        
        starImageView.snp.makeConstraints { make in
            make.width.height.equalTo(8*widthScale)
            make.left.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(8*widthScale)
        }
        
        scoreLabel.snp.makeConstraints { make in
            make.left.equalTo(starImageView.snp.right)
            make.centerY.equalTo(starImageView)
        }
        
        typeLabel.snp.makeConstraints { make in
            make.left.equalTo(scoreLabel.snp.right).offset(6*widthScale)
            make.centerY.equalTo(scoreLabel)
        }
        
        sizeLabel.snp.makeConstraints { make in
            make.left.equalTo(typeLabel.snp.right).offset(13*widthScale)
            make.centerY.equalTo(typeLabel)
        }
        
        textTagCollectionView.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.right.equalTo(0)
            make.bottom.equalTo(logoImageView)
            //            make.top.equalTo(starImageView.snp.bottom).offset(4*widthScale)
        }
        
        detailLabel.snp.makeConstraints { make in
            make.centerY.equalTo(logoImageView)
            make.right.equalTo(-12*widthScale)
            make.width.equalTo(40*widthScale)
            make.height.equalTo(26*widthScale)
        }
        
        onSaleLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}

extension HWGameClassifyGameCell:TTGTextTagCollectionViewDelegate{
    func textTagCollectionView(_ textTagCollectionView: TTGTextTagCollectionView!, didTap tag: TTGTextTag!, at index: UInt) {
        //        tagIsSelect = tag.selected
        //        for i in 0..<_model!.datas.count {
        //
        //            textTagCollectionView.updateTag(at: UInt(i), selected: false)
        //            _model?.datas[i].isSelect = false
        //        }
        //        textTagCollectionView.updateTag(at: index, selected: tagIsSelect)
        //        _model?.datas[Int(index)].isSelect = tagIsSelect
        
    }
}

extension HWGameClassifyGameCell {
    @objc func imageViewTap(_:UITapGestureRecognizer) -> Void {
        self.gameImageViewClick?(self.centerImageView,model ?? HWClassifyGameModel(),self)
    }
}
